/**
 * 文件描述：基础信息
 * 创建时间：2023/9/5 16:16
 * 创建人：二
 */
import router from '@/router';
const base = {
	namespaced: true,
	state: () => ({
		// 是否折叠收起菜单 true收起
		isCollapse: false,
		// 导航标签
		tagNavList: [],
		// 当前路由
		currentRoute: null,
		// 缓存
		keepAliveView: [],
		// 左侧导航激活项
		menuActiveIndex: '/home'
	}),
	mutations: {
		// 侧边栏展开/收起
		setIsCollapse(state) {
			state.isCollapse = !state.isCollapse
		},
		// 设置导航菜单激活项
		setMenuActiveIndex(state, link) {
			let route = state.currentRoute;
			if(link) {
				state.menuActiveIndex = link
			}else {
				state.menuActiveIndex = route.meta?.activeMenu || route.meta.url;
			}
		},
		// 设置当前路由
		setCurrentRoute(state, data) {
			state.currentRoute = {
				fullPath: data.fullPath,
				hash: data.hash,
				name: data.name,
				path: data.path,
				query: data.query,
				meta: data.meta,
				params: data.params
			};
		},
		// 初始化TagNavList
		initTagNavList(state, data = []) {
			state.tagNavList = data
		},
		// 添加 导航标签
		addTagNavList(state) {
			let route = state.currentRoute;
			let index = state.tagNavList.findIndex(d => d.path === route.path)
			if(index !== -1) {
				state.tagNavList.splice(index, 1, route)
			}else {
				state.tagNavList.push(route)
			}
		},
		// 删除导航标签
		delTagNavList(state, route) {
			let index = state.tagNavList.findIndex(d => d.path === route.path);
			if(index !== -1) {
				state.tagNavList.splice(index, 1)
			}
		},
		// 初始化KeepAliveView
		initKeepAliveView(state, data = []) {
			state.keepAliveView = data
		},
		// 添加缓存
		addKeepAliveView(state) {
			let route = state.currentRoute;
			if(!state.keepAliveView.includes(route.name) && route.meta.keepAlive) {
				state.keepAliveView.push(route.name)
			}
		},
		// 删除缓存
		delKeepAliveView(state, route) {
			let index = state.keepAliveView.findIndex(d => d === route.name);
			if(index !== -1) {
				state.keepAliveView.splice(index, 1)
			}
		}
	},
	actions: {
		// 路由改变
		watchRoute({commit}, to) {
			commit('setCurrentRoute', to);
			commit('setMenuActiveIndex');
			commit('addTagNavList');
			commit('addKeepAliveView');
		},
		/**
		 * 删除导航标签
		 * @param state
		 * @param commit
		 * @param closeRoute 删除指定TabNav，不设置关闭当前TabNav
		 * @param to 指定跳转路由，不设置自动打开删除项左侧的TabNav
		 */
		closeTabNav({state, commit}, { closeRoute, to }) {
			const route = closeRoute || state.currentRoute;
			const currentRoute = router.currentRoute;
			const tagNavList = state.tagNavList;
			const index = tagNavList.findIndex(item => item.path === route.path);
			commit('delTagNavList', route)
			commit('delKeepAliveView', route)

			if(to) {
				commit('delKeepAliveView', to);
				router.push(to).then();
			}else {
				if(route.path === currentRoute.path) {
					let newRoute = tagNavList[index - 1] || {};
					const { path = '/', query = {} } = newRoute;
					router.push({ path, query }).then();
				}
			}
		},
		// 关闭所有导航标签
		closeAllTabNav({state, commit}) {
			const currentRoute = router.currentRoute;
			const newTagNavList = state.tagNavList.filter(item => item.meta.notClose);
			commit('initTagNavList', newTagNavList);

			const newKeepAliveView = newTagNavList.filter(item => item.meta.keepAlive && item.name)
			commit('initKeepAliveView', newKeepAliveView)

			const { path = '/', query = {} } = newTagNavList[0];
			if(newTagNavList[0] && newTagNavList[0].path !== currentRoute.path) {
				router.push({ path, query }).then()
			}
		},
		// 除当前标签，其他都关闭
		closeOthersTabNav({state, commit}) {
			const currentRoute = router.currentRoute;
			const newTagNavList = state.tagNavList.filter(item => item.meta.notClose || item.path === currentRoute.path);
			commit('initTagNavList', newTagNavList);

			const newKeepAliveView = newTagNavList.filter(item => item.meta.keepAlive && item.name)
			commit('initKeepAliveView', newKeepAliveView)
		},
		// 删除缓存重新加载
		async delKeepAliveViewReload({commit, state}, route) {
			await commit('delKeepAliveView', route);
			router.push(route).then()
		}
	}
}
export default base
